package com.umeox.iqibla.custom_tasbih.ui

import android.annotation.SuppressLint
import android.content.Intent
import android.database.ContentObserver
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.RemoteException
import android.view.View
import android.widget.LinearLayout
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.umeox.iqibla.custom_tasbih.R
import com.umeox.iqibla.custom_tasbih.adapter.CustomTasbihTaskListAdapter
import com.umeox.iqibla.custom_tasbih.data.TasbihTaskListManager
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity
import com.umeox.moto.watch.themes.app.ThemeManager
import com.umeox.moto.watch.themes.widget.TextView
import com.umeox.watch.moto.apiclient.model.TaskRecordDTO
import com.umeox.watch.moto.apiclient.model.TaskRecordInfoDTO
import com.umeox.watch.moto.dataservice.DataProvider
import com.umeox.watch.moto.dataservice.proxy.PushApiClientProxy
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class MainActivity : ThemeAppCompatActivity(), CustomTasbihTaskListAdapter.OnItemClickListener {

    private val adapter = CustomTasbihTaskListAdapter()

    private lateinit var tvNumber:TextView

    private val contentObserver: ChantsCounterChallengeTotalContentObserver =
        ChantsCounterChallengeTotalContentObserver()

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val btRanking = findViewById<LinearLayout>(R.id.bt_ranking)
        btRanking.setOnClickListener {
            startActivity(Intent(this, TaskRankingActivity::class.java))
        }
        tvNumber = findViewById(R.id.tvNumber)

        val llNoData = findViewById<LinearLayout>(R.id.ll_no_data)
        DataProvider.HolderInfoProvider.registerContentObserver(true, contentObserver)

        val mStepRv: RecyclerView = findViewById(R.id.rv_custom_tasbih)
        val linearLayoutManager = LinearLayoutManager(mContext)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        mStepRv.layoutManager = linearLayoutManager
        mStepRv.adapter = adapter
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            mStepRv.isFocusableInTouchMode = false
            mStepRv.requestFocus()
        }

        adapter.setOnItemClickListener(this)
        tvNumber.text = "${DataProvider.getChantsCounterChallengeTotal() + DataProvider.getChantsCounterTotal()}"

        TasbihTaskListManager.taskListObservable.observe(this) {
            if (it.isEmpty()) {
                llNoData.visibility = View.VISIBLE
                mStepRv.visibility = View.GONE
            } else {
                adapter.setDataList(it)
                llNoData.visibility = View.GONE
                mStepRv.visibility = View.VISIBLE
            }
        }
    }

    override fun onResume() {
        super.onResume()
        loadData()
    }

    private fun loadData() {
        lifecycleScope.launch(Dispatchers.IO) {
            delay(1000)
            try {
                val result = getTaskRecord()
                TasbihTaskListManager.updateTaskList(result)
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

    @Throws(RemoteException::class)
    fun getTaskRecord(): TaskRecordInfoDTO {
        return PushApiClientProxy.fetchBinder().taskRecord
    }

    override fun onItemClick(position: Int, data: TaskRecordDTO) {
        TaskActivity.intoTaskActivity(this, position)
    }

    override fun onDestroy() {
        super.onDestroy()
        contentResolver.unregisterContentObserver(contentObserver)
    }

    inner class ChantsCounterChallengeTotalContentObserver : ContentObserver(Handler()) {
        @SuppressLint("SetTextI18n")
        override fun onChange(selfChange: Boolean) {
            super.onChange(selfChange)
            tvNumber.text = "${DataProvider.getChantsCounterChallengeTotal() + DataProvider.getChantsCounterTotal()}"
        }
    }
}